package net.anzhuo.controller.db.report;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.entity.PaginationVO;
import net.anzhuo.common.utils.JsonUtils;
import net.anzhuo.common.utils.RedisUtil;
import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsCopyRight;
import net.anzhuo.entity.GsZhuzuoquan;
import net.anzhuo.entity.db.zhuozuoquan.Paging;
import net.anzhuo.entity.db.zhuozuoquan.ResponseResult;
import net.anzhuo.entity.db.zhuozuoquan.Result;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsCopyRightService;
import net.anzhuo.service.GsZhuzuoquanService;

@RestController
@Slf4j
public class ZhuzuoquanController {

	@Autowired
	private GsZhuzuoquanService gsZhuzuoquanService;

	@Autowired
	private GsCopyRightService gsCopyRightService;
	
	@Autowired
	private GsBaseService gsBaseService;
	
	@RequestMapping("CopyRight/SearchSoftwareCr")
	public ResponseResult SearchSoftwareCr(String searchKey, String pageSize) {
		log.info("===>SearchSoftwareCr_searchKey="+searchKey+", pageSize="+pageSize);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
		
		ResponseResult rr = null;
		try {
			rr = (ResponseResult)RedisUtil.get("SearchSoftwareCr:"+searchKey);
			if (rr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" SearchSoftwareCr 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsZhuzuoquan gsZhuzuoquanQuery = new GsZhuzuoquan();
	
		gsZhuzuoquanQuery.setCoMd5(searchKey);
		gsZhuzuoquanQuery.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsZhuzuoquan> zhuzuoquanList = gsZhuzuoquanService.getListWithPage(gsZhuzuoquanQuery);
		
		ResponseResult responseResult = new ResponseResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		Paging paging = new Paging();
		paging.setPageIndex(1);
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(zhuzuoquanList.getTotalCount());
		responseResult.setPaging(paging);
		
		GsBase base=gsBaseService.getByMd5(searchKey);
		List<Result> resultList = new ArrayList<>();
		Result result = null;
		if (zhuzuoquanList.getList()!=null && zhuzuoquanList.getList().size()>0) {
			for (GsZhuzuoquan z : zhuzuoquanList.getList()) {
				result = new Result();
				result.setCategory(z.getClassNumber());
				result.setRegisterNo(z.getRegNumber());
				result.setName(z.getFullName());
				result.setShortName(z.getSimpleName());
				result.setOwner(base.getName());
				result.setVersionNo(z.getVersionNumber());
				String detail = z.getDetail();
				String publishDate = "";
				if (StringUtils.isNotBlank(detail)) {
					publishDate = (String)JsonUtils.jsonToMap(detail).get("regtime");
					if (StringUtils.isNotBlank(publishDate)) {
						publishDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date(Long.parseLong(publishDate)));
					}
				}
				result.setPublishDate(publishDate);
				result.setRegisterAperDate(z.getApprovalTime());
				resultList.add(result);
			}
		}
		responseResult.setResult(resultList);
		
		try {
			RedisUtil.set("SearchSoftwareCr:"+searchKey, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" SearchSoftwareCr 执行时间："+excTime+"s");
		return responseResult;
	}
	
	@RequestMapping("CopyRight/GetCopyRight")
	public net.anzhuo.entity.db.copyright.ResponseResult getZhuzuoquan(String searchKey, String pageSize) {
		log.info("===>GetCopyRight_searchKey="+searchKey+", pageSize="+pageSize);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
		
		net.anzhuo.entity.db.copyright.ResponseResult rr = null;
		try {
			rr = (net.anzhuo.entity.db.copyright.ResponseResult)RedisUtil.get("GetCopyRight:"+searchKey);
			if (rr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" GetCopyRight 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsCopyRight gsZhuzuoquanQuery = new GsCopyRight();
	
		
		
		GsBase base=gsBaseService.getByMd5(searchKey);
		gsZhuzuoquanQuery.setCoMd5(searchKey);
		gsZhuzuoquanQuery.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsCopyRight> zhuzuoquanList = gsCopyRightService.getListWithPage(gsZhuzuoquanQuery);
		
		net.anzhuo.entity.db.copyright.ResponseResult responseResult = new net.anzhuo.entity.db.copyright.ResponseResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		net.anzhuo.entity.db.copyright.Paging paging = new net.anzhuo.entity.db.copyright.Paging();
		paging.setPageIndex(1);
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(zhuzuoquanList.getTotalCount());
		responseResult.setPaging(paging);
		
		List<net.anzhuo.entity.db.copyright.Result> resultList = new ArrayList<>();
		net.anzhuo.entity.db.copyright.Result result = null;
		if (zhuzuoquanList.getList()!=null && zhuzuoquanList.getList().size()>0) {
			for (GsCopyRight z : zhuzuoquanList.getList()) {
				result = new net.anzhuo.entity.db.copyright.Result();
				result.setCategory(z.getType());
				result.setName(z.getName());
				result.setOwner(base.getName());
				result.setPublishDate(z.getPublicTime());
				result.setRegisterDate(z.getRegTime());
				result.setRegisterNo(z.getRegNumber());
				resultList.add(result);
			}
		}
		responseResult.setResult(resultList);
		
		try {
			RedisUtil.set("GetCopyRight:"+searchKey, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" GetCopyRight 执行时间："+excTime+"s");
		return responseResult;
	}
}
